﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

public partial class BookInformation : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        
        txtUserName.Text = "Comment as: "+Session.GetCurrentUser();
        if (Session.GetCurrentUser( ) == null)
        {
            txtUserName.Text = "You didn't login!";
            boxComment.ReadOnly = true ;
            btnComment.Enabled = false;
        }
        string id = Request.Params["BookID"];
        LINQDataContext info = new LINQDataContext();
        var infoPage = from a in info.GetTable<Book>()
                       where a.BookID.ToString() == id
                       select a;
        
        foreach (var b in infoPage)
        {
            if (b.Cover == null) Image1.ImageUrl = "/Images/no_photo.jpg";
            else
            {
                Image1.ImageUrl = b.Cover;
            }

            Label1.Text = b.BookName;
            txtBookCode.Text = ""+b.BookID;
            txtAuthor.Text = b.Author;
            txtCatalogue.Text = b.Catalogue;
            txtPublisher.Text = b.Publisher;
            txtPublishYear.Text = ""+b.PublishYear;
            if (b.Description == null) txtDescription.Text = "No Description";
            else
                txtDescription.Text = ""+b.Description;
        }

        
      


    }
    public void DisplayComment()
    {
        string id = Request.Params["BookID"];
        LINQDataContext cm = new LINQDataContext();

        var cmbox = from a in cm.GetTable<Comment>()
                    where a.BookID.ToString() == id
                    select a;


        if (Session.GetCurrentUser( ) != "Admin")
        {

            foreach (var b in cmbox)
            {

                Response.Write( String.Format( "<span class=\"infor-commentauthor\">--{0} said:</span><br/>", b.UserName ) );

                Response.Write( String.Format( "<span class=\"infor-commentbody\">{0}</span><br/>", b.Comment1 ) );


            }
        }
        else
        {


            foreach (var b in cmbox)
            {

                Response.Write( String.Format( "<span class=\"infor-commentauthor\">--{0} said:</span><br/>", b.UserName ) );

                Response.Write(String.Format("<span class=\"infor-commentbody\">{0}</span><a href=\"deletecomment.aspx?CommentID={1}&BookID={2}\" cssclass=\"infor-commentbody\" >[Delete]</a><br/>", b.Comment1, b.CommentID, b.BookID));


            }
        }
        
    }

    protected void txtUserName_TextChanged(object sender, EventArgs e)
    {

    }
    protected void btnComment_Click(object sender, EventArgs e)
    {
       string id = Request.Params["BookID"];
       LINQDataContext cm = new LINQDataContext();
        
        Comment newComment = new Comment
        {
            UserName=Session.GetCurrentUser(),
            BookID=int.Parse(Request.Params["BookID"]),
            Comment1=boxComment.Text

        };
        cm.Comments.InsertOnSubmit(newComment);
        cm.SubmitChanges();

        Response.Redirect("BookInformation.aspx?BookID=" + id);




        //Page_Load(sender,e);
       
    }
}